<?php
$cache_time=30;
$OJ_CACHE_SHARE=false;
$cache_time=60;
require_once("config/path_config.php");
require_once("common/db_utils.php");
require_once('./include/cache_start.php');
require_once('./include/db_info.inc.php');
require_once('./include/setlang.php');
require_once('./config/sys_config.php');

// 判断用户是否已登录
if ( !isset($_SESSION['user_id']) ) {
    $view_errors= "<a href=loginpage.php>$MSG_Login</a>";
    require("template/bs3/error.php");
    exit(0);
}

require_once("./include/db_info.inc.php");
require_once("./lang/cn.php");

if (isset($_GET['id'])){
    // 查看指定题目的信息
    $id = intval( $_GET['id'] );
    $args=array($id);
    $sql = null;
    if ( isset($_SESSION['administrator']) ) {
        // 1. 超级管理员可以查看任意题目
        $sql="SELECT * FROM `problem` WHERE `problem_id`=?";
    }
    else if ( isset($_SESSION['problem_editor']) ) {
        // 2. 题目创建者可以查看自己创建的题目
        $sql="SELECT * FROM `problem` WHERE `problem_id`=? and (`creater_id`=? OR `defunct`='N')";
        array_push($args, $_SESSION['user_id']);
    }
    else {
        // 3. 其他用户可查看任意已经被启用的题目
        $sql="SELECT * FROM `problem` WHERE `problem_id`=? and `defunct`='N'";
    }
    $result = DbUtil::getInstance()->query2($sql, $args);
}else if (isset($_GET['cid']) && isset($_GET['pid'])){
    // 查看指定比赛的题目信息
    $cid=intval($_GET['cid']);
    $pid=intval($_GET['pid']);

    // $now = strftime("%Y-%m-%d %H:%M",time());
    if (!isset($_SESSION['administrator'])) {
        $sql="SELECT langmask,private,defunct FROM `contest` WHERE `defunct`='N' AND `contest_id`=? AND `start_time`<='$now'";
    }
    else {
        $sql="SELECT langmask,private,defunct FROM `contest` WHERE `defunct`='N' AND `contest_id`=?";
    }
    $result=pdo_query($sql,$cid);
    $rows_cnt=count($result);
    $row=($result[0]);
    $contest_ok=true;
    if ($row[1] && !isset($_SESSION['c'.$cid])) {
        $contest_ok=false;
    }
    if ($row[2]=='Y') {
        $contest_ok=false;
    }
    if (isset($_SESSION['administrator'])) {
        $contest_ok=true;
    }

    $ok_cnt=$rows_cnt==1;
    $langmask=$row[0];
    if ($ok_cnt!=1) {
        $view_errors=  "No such Contest!";
        require("template/".$OJ_TEMPLATE."/error.php");
        exit(0);
    }else{
        $sql="SELECT * FROM `problem` WHERE `defunct`='N' AND `problem_id`=(
                        SELECT `problem_id` FROM `contest_problem` WHERE `contest_id`=? AND `num`=?
                        )";
        $result=pdo_query($sql,$cid,$pid);
    }

    if (!$contest_ok){
        $view_errors= "Not Invited!";
        require("template/bs3/error.php");
        exit(0);
    }
}else{
    // 参数错误
    $view_errors=  "<title>题目不可用!</title><h2>题目不可用!</h2>";
    require("template/bs3/error.php");
    exit(0);
}

if ( count($result)==1 ) {
    $row=$result[0];
    $view_title= $row['title'];
}
else {
    $view_errors="";
    if(isset($_GET['id'])) {
        $id=intval($_GET['id']);
        $sql="SELECT  contest.`contest_id` , contest.`title`,contest_problem.num FROM `contest_problem`,`contest` 
				WHERE contest.contest_id=contest_problem.contest_id and `problem_id`=? and defunct='N'  ORDER BY `num`";

        $result=pdo_query($sql,$id);
        if($i=count($result)){
            $view_errors.= "This problem is in Contest(s) below:<br>";
            foreach($result as $row){
                $view_errors.= "<a href=problem.php?cid=$row[0]&pid=$row[2]>Contest $row[0]:".
                    htmlentities($row[1],ENT_QUOTES,"utf-8")
                    ."</a><br>";
            }
        }else{
            $view_title= "<title>$MSG_NO_SUCH_PROBLEM!</title>";
            $view_errors.= "<h2>$MSG_NO_SUCH_PROBLEM!</h2>";
        }
    }else{
        $view_title= "<title>$MSG_NO_SUCH_PROBLEM!</title>";
        $view_errors.= "<h2>$MSG_NO_SUCH_PROBLEM!</h2>";
    }
    require("template/bs3/error.php");
    exit(0);
}

$view_df_change = "";
// 1. 管理员
$allowDfChg = isset($_SESSION['administrator']);
$allowDfChg |= isset($_SESSION['problem_editor'])
    && (strtolower($row['creater_id']) == strtolower($_SESSION['user_id']));
if ( $allowDfChg ){
    // 生成操作密码
    require_once("./include/set_get_key.php");
    if ( $row['defunct'] == 'N' ) {
        $view_df_change="[<a href='/admin/problem_df_change.php?id=".$row['problem_id']
            ."&getkey=".$_SESSION['getkey']
            ."'><span class=zz_green>已启用</span></a>]";
    }
    else {
        $view_df_change="[<a href='/admin/problem_df_change.php?id=".$row['problem_id']
            ."&getkey=".$_SESSION['getkey']
            ."'><span class=zz_red>已禁用</span></a>]";
    }
}

// 将类别拆分成题目类型type和算法标签
// add 2019-10-23 by swb
$category = $row['source'];
$problem_type="";
$pos = strpos($category, '(');
if ( $pos === false ) {
    $problem_type = $category;
}
else {
    /**
    获取题目类型,分为:
    (1) 程序设计基础
    (2) 基础算法
    (3) 基础数据结构
    (4) 高阶算法
    (5) 高阶数据结构
     */
    $problem_type = substr($category, 0, $pos);
    // 获取算法
    $label_str = substr($category, $pos+1, -1);
    $problem_labes = explode("、", $label_str);
}

/////////////////////////Template
require("template/bs3/problem.php");
/////////////////////////Common foot
if(file_exists('./include/cache_end.php')) {
    require_once('./include/cache_end.php');
}
require("template/bs3/oj-footer.php");
?>

